package com.taotao.portal.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbContent;
import com.taotao.portal.pojo.AdNode;

@Service
public class ContentServiceImpl implements ContentService {
	// 将请求服务端大广告位内容的URL分解为属性放在属性文件中,方便以后修改
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_CONTENT_URL}")
	private String REST_CONTENT_URL;
	@Value("${REST_CONTENT_AD1_CID}")
	private String REST_CONTENT_AD1_CID;

	@Override
	public String getAd1List() {
		String url = REST_BASE_URL + REST_CONTENT_URL + REST_CONTENT_AD1_CID;
		// 从服务端获得json数据
		String contents = HttpClientUtil.doGet(url);
		// 将字符串转换成TaotaoResult对象,并取出data转换为list
		TaotaoResult result = TaotaoResult.formatToList(contents,
				TbContent.class);
		List<TbContent> contentList = (List<TbContent>) result.getData();
		List<AdNode> resultList = new ArrayList<>();
		// 遍历tbContent内容,转换为AdNode对象
		for (TbContent tbContent : contentList) {
			AdNode node = new AdNode();
			node.setAlt(tbContent.getTitle());
			node.setSrc(tbContent.getPic());
			node.setHeight(240);
			node.setWidth(670);
			node.setSrcB(tbContent.getPic2());
			node.setHeightB(240);
			node.setWidthB(550);
			node.setHref(tbContent.getUrl());

			resultList.add(node);
		}
        //将resultList转换为json数据
		String adList = JsonUtils.objectToJson(resultList);
		return adList;
	}

}
